package com.qch.rocketmq.springboot_ms_rocketmq.controller;

import com.qch.rocketmq.springboot_ms_rocketmq.pojo.GoodDO;
import com.qch.rocketmq.springboot_ms_rocketmq.pojo.OrderDO;
import com.qch.rocketmq.springboot_ms_rocketmq.service.GoodsService;
import com.qch.rocketmq.springboot_ms_rocketmq.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author qch
 * @Date 2021/6/23
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @RequestMapping("/payPage/{userId}/{orderId}")
    public ModelAndView payPage(@PathVariable("orderId") String orderId,@PathVariable("userId") String userId ) {
        ModelAndView modelAndView=new ModelAndView("/order.html");
        OrderDO byOrderId = orderService.findByOrderId(orderId);
        if (byOrderId==null)
        {
            modelAndView.setViewName("/nogoods.html");
        }else {
            modelAndView.addObject("order", byOrderId);
        }
        return modelAndView;
    }
    @RequestMapping("/pay/{orderId}")
    public ModelAndView payPage(@PathVariable("orderId") String orderId ) {
        ModelAndView modelAndView=new ModelAndView("/success.html");
        OrderDO byOrderId = orderService.findByOrderId(orderId);
        if (byOrderId==null)
        {
            modelAndView.addObject("msg","订单不存在");
        }else {
            if(byOrderId.getStatus().equals(0)){
                byOrderId.setPayTime(new Date());
                byOrderId.setStatus(1);
                orderService.save(byOrderId);
                modelAndView.addObject("msg","支付成功");
            }else if(byOrderId.getStatus().equals(1)){
                modelAndView.addObject("msg","订单已支付完成，请勿重复操作");
            }else {
                modelAndView.addObject("msg","订单超时已取消");
            }

        }
        return modelAndView;
    }
}
